package com.ruoyi.web.controller.system.city;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.city.CityTrade;
import com.ruoyi.system.domain.city.CityTradeSecond;
import com.ruoyi.system.domain.city.vo.TradeOptionVo;
import com.ruoyi.system.domain.city.vo.TradeSecondOptionVo;
import com.ruoyi.system.service.city.ICityTradeSecondService;
import com.ruoyi.system.service.city.ICityTradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RequestMapping({"/system/city/trade"})
@RestController
/* loaded from: CityTradeController.class */ public class CityTradeController extends BaseController {
    @Autowired
    private ICityTradeService       cityTradeService;
    @Autowired
    private ICityTradeSecondService cityTradeSecondService;

    @GetMapping({"/list"})
    @PreAuthorize("@ss.hasPermi('city:trade:list')")
    public TableDataInfo list(CityTrade cityTrade) {
        startPage();
        return getDataTable(this.cityTradeService.selectCityTradeList(cityTrade));
    }

    @GetMapping({"/tradeList"})
    public List<CityTrade> tradeList(CityTrade cityTrade) {
        startPage();
        return this.cityTradeService.selectCityTradeList(cityTrade);
    }

    @GetMapping({"/tradeOptions"})
    public List<TradeOptionVo> tradeOptions(CityTrade cityTrade) {
        startPage();
        List<CityTrade>     list              = this.cityTradeService.selectCityTradeList(cityTrade);
        List<TradeOptionVo> tradeOptionVoList = new ArrayList<>();
        if (!list.isEmpty()) {
            for (CityTrade c : list) {
                TradeOptionVo tradeOptionVo = new TradeOptionVo();
                tradeOptionVo.setValue(c.getTradeName());
                tradeOptionVo.setLabel(c.getTradeName());
                CityTradeSecond cityTradeSecond = new CityTradeSecond();
                cityTradeSecond.setTradeId(c.getTradeId());
                List<CityTradeSecond>     cityTradeSecondList     = this.cityTradeSecondService.selectCityTradeSecondList(cityTradeSecond);
                List<TradeSecondOptionVo> tradeSecondOptionVoList = new ArrayList<>();
                if (!cityTradeSecondList.isEmpty()) {
                    for (CityTradeSecond s : cityTradeSecondList) {
                        TradeSecondOptionVo tradeSecondOptionVo = new TradeSecondOptionVo();
                        tradeSecondOptionVo.setValue(s.getTradeSecondName());
                        tradeSecondOptionVo.setLabel(s.getTradeSecondName());
                        tradeSecondOptionVoList.add(tradeSecondOptionVo);
                    }
                }
                tradeOptionVo.setChildren(tradeSecondOptionVoList);
                tradeOptionVoList.add(tradeOptionVo);
            }
        }
        return tradeOptionVoList;
    }

    @Log(title = "一级行业", businessType = BusinessType.EXPORT)
    @PostMapping({"/export"})
    @PreAuthorize("@ss.hasPermi('city:trade:export')")
    public void export(HttpServletResponse response, CityTrade cityTrade) {
        new ExcelUtil<>(CityTrade.class).exportExcel(response, this.cityTradeService.selectCityTradeList(cityTrade), "一级行业数据");
    }

    @GetMapping({"/{tradeId}"})
    @PreAuthorize("@ss.hasPermi('city:trade:query')")
    public AjaxResult getInfo(@PathVariable("tradeId") Long tradeId) {
        return success(this.cityTradeService.selectCityTradeByTradeId(tradeId));
    }

    @Log(title = "一级行业", businessType = BusinessType.INSERT)
    @PostMapping
    @PreAuthorize("@ss.hasPermi('city:trade:add')")
    public AjaxResult add(@RequestBody CityTrade cityTrade) {
        cityTrade.setTradeId(Long.valueOf(IdUtil.getSnowflake(1, 1).nextId()));
        return toAjax(this.cityTradeService.insertCityTrade(cityTrade));
    }

    @Log(title = "一级行业", businessType = BusinessType.UPDATE)
    @PutMapping
    @PreAuthorize("@ss.hasPermi('city:trade:edit')")
    public AjaxResult edit(@RequestBody CityTrade cityTrade) {
        return toAjax(this.cityTradeService.updateCityTrade(cityTrade));
    }

    @Log(title = "一级行业", businessType = BusinessType.DELETE)
    @DeleteMapping({"/{tradeIds}"})
    @PreAuthorize("@ss.hasPermi('city:trade:remove')")
    public AjaxResult remove(@PathVariable Long[] tradeIds) {
        return toAjax(this.cityTradeService.deleteCityTradeByTradeIds(tradeIds));
    }
}
